<?php
/*
          U1,                                               
          LMF1Yr,                                           
          BOOMBBBBBBq                                       
         7BEP0PNqZBM                                        
         B8kPFXFX0B                                         
        YBXXSSFkSB;     APUXIN
        Bqk1k5SFNZ      Web OS
     :r0Ek5kSXSXO;      
   jBBBNPkXF5uUYF:      by Apuxin Team
  BBqPkXUj7rrr;rrur    
.J8FX5u7r;rr7r7r7rUi    sessions.service.php: Manage the session
0UkkJ7irr7rrr7rrrrrj           
PuX7;i7;r;r;rrririrL.           
 7L;;rrrrriri;i;i;iL           
  YL7;riri;i;iii;iL:  
   ;LLrr;ri;i;irr7.                                         
       ...... .      

*/

/**
* This service is responsible for starting a session, managing a session and set/get values out of a session.
* Attention: Start this service before an output is done!
*/

class sessions extends APUXIN_SERVICE
{

    public function __construct()
    {
        parent::__construct();
        $this->info_setName('sessions');
        $this->info_setDesc('This service is responsible for starting a session and managing its state');
        $this->info_setVersion(0.1);
        $this->info_setOnlyOnce(true);
    }

    public function getId()
    {
        if(session_id() == "")
        {
            $this->startSession();
        }
		return session_id();
    }

    public function setId($id)
    {
        if(session_id() == "")
        {
            $this->startSession();
        }
        return session_id($id);
    }

    public function getName()
    {
        if(session_id() == "")
        {
            $this->startSession();
        }

        return session_name();
    }
    
    public function setName($name)
    {
        if(session_id() == "")
        {
            $this->startSession();
        }
        
        return session_name($name);
    }
    
    public function getValue($key)
    {
        if(session_id() == "")
        {
            $this->startSession();
        }
	
        return $_SESSION[$key];
    }
    
    public function setValue($key, $value)
    {
        if(session_id() == "")
        {
        	$this->startSession();
        }
	
        $_SESSION[$key] = $value;
        if($_SESSION[$key] == $value)
        {
            return true;
        } else
        {
            return false;
        }
    }
    
    public function rmValue($key)
    {
      if(session_id() == "") return false;
      if(!$this->is_set($key)) return false;
      unset($_SESSION[$key]);
      return true;
    }
    
    public function is_set($key)
    {
      return isset($_SESSION[$key]);
    }
    
    public function blank($key)
    {
      return (!$this->is_set($key) || $this->getValue($key) == "");
    }
    
    public function clear()
    {
      foreach($_SESSION as $k=>$v) {
        $this->rmValue($k);
      }
    }
    
    public function stopSession()
    {
        if(session_id() == "")
        {
            $this->startSession();
        }
	
        session_unset();
        if(isset($_COOKIE[session_name()]))
        {
        	setcookie(session_name(), "", time()-86400);
        }
        session_destroy();
    }
    
    public function startSession()
    {
        if(NEED_COOKIES === true)
        {
            @ini_set("session.use_cookies", 1);
            @ini_set("session.use_only_cookies", 1);
            @ini_set("session.use_trans_sid", 0);
        }
        session_start() or throw_error("Session cannot be started. Maybe the header is already sent?", FATAL_ERROR);
    }
}
?>